package com.abo.yaas.view.widgets.tables;

import com.abo.yaas.view.widgets.LocaleSensitiveLabel;
import com.abo.yaas.view.widgets.tables.YaasPagedTable.PageChangeListener;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.data.validator.IntegerValidator;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextField;
import com.vaadin.ui.themes.BaseTheme;
import com.vaadin.ui.themes.Reindeer;

@SuppressWarnings("serial")
public class PagedTableControls extends HorizontalLayout implements
        PageChangeListener, ClickListener {
    private final Button first = new Button("<<", this, "buttonClick");

    private final Button previous = new Button("<", this, "buttonClick");

    private final Button next = new Button(">", this, "buttonClick");

    private final Button last = new Button(">>", this, "buttonClick");

    private final TextField currentPageTextField = new TextField();

    private Label totalPagesLabel = null;

    private final Label pageLabel = new LocaleSensitiveLabel("PAGE_LABEL");

    private final Label separatorLabel = new Label("&nbsp;/&nbsp;",
            Label.CONTENT_XHTML);

    final IPagedComponent navigator;

    public PagedTableControls(final IPagedComponent navigator) {
        super();
        setWidth("100%");

        this.navigator = navigator;

        currentPageTextField.setStyleName(Reindeer.TEXTFIELD_SMALL);
        currentPageTextField.setImmediate(true);
        currentPageTextField
                .setValue(String.valueOf(navigator.getCurrentPage()));
        currentPageTextField.addValidator(new IntegerValidator(null));
        currentPageTextField.addListener(new ValueChangeListener() {
            @Override
            public void valueChange(
                    com.vaadin.data.Property.ValueChangeEvent event) {
                if (currentPageTextField.isValid()
                        && currentPageTextField.getValue() != null) {
                    final int page = Integer.valueOf(String
                            .valueOf(currentPageTextField.getValue()));
                    navigator.setCurrentPage(page);
                }
            }
        });

        pageLabel.setWidth(null);
        currentPageTextField.setWidth("20px");
        totalPagesLabel = new Label(String.valueOf(navigator.getPageAmount()),
                Label.CONTENT_XHTML);
        separatorLabel.setWidth(null);
        totalPagesLabel.setWidth(null);

        final HorizontalLayout pageSize = new HorizontalLayout();
        final HorizontalLayout pageManagement = new HorizontalLayout();

        first.setStyleName(BaseTheme.BUTTON_LINK);
        previous.setStyleName(BaseTheme.BUTTON_LINK);
        next.setStyleName(BaseTheme.BUTTON_LINK);
        last.setStyleName(BaseTheme.BUTTON_LINK);

        pageSize.setSpacing(true);
        pageManagement.addComponent(first);
        pageManagement.addComponent(previous);
        pageManagement.addComponent(pageLabel);
        pageManagement.addComponent(currentPageTextField);
        pageManagement.addComponent(separatorLabel);
        pageManagement.addComponent(totalPagesLabel);
        pageManagement.addComponent(next);
        pageManagement.addComponent(last);
        pageManagement.setComponentAlignment(first, Alignment.MIDDLE_LEFT);
        pageManagement.setComponentAlignment(previous, Alignment.MIDDLE_LEFT);
        pageManagement.setComponentAlignment(pageLabel, Alignment.MIDDLE_LEFT);
        pageManagement.setComponentAlignment(currentPageTextField,
                Alignment.MIDDLE_LEFT);
        pageManagement.setComponentAlignment(separatorLabel,
                Alignment.MIDDLE_LEFT);
        pageManagement.setComponentAlignment(totalPagesLabel,
                Alignment.MIDDLE_LEFT);
        pageManagement.setComponentAlignment(next, Alignment.MIDDLE_LEFT);
        pageManagement.setComponentAlignment(last, Alignment.MIDDLE_LEFT);
        pageManagement.setWidth(null);
        pageManagement.setSpacing(true);

        addComponent(pageSize);
        addComponent(pageManagement);
        setComponentAlignment(pageManagement, Alignment.MIDDLE_CENTER);
        setExpandRatio(pageSize, 1);
    }

    @Override
    public void buttonClick(ClickEvent event) {
        final Button button = (Button) event.getComponent();
        if (button.equals(first)) {
            navigator.setCurrentPage(0);
        } else if (button.equals(next)) {
            navigator.nextPage();
        } else if (button.equals(previous)) {
            navigator.previousPage();
        } else if (button.equals(last)) {
            navigator.setCurrentPage(navigator.getPageAmount());
        }
    }

    @Override
    public void pageChanged(PagedTableChangeEvent event) {
        final IPagedComponent navigator = event.getPagedComponent();
        previous.setEnabled(true);
        next.setEnabled(true);
        currentPageTextField
                .setValue(String.valueOf(navigator.getCurrentPage()));
        totalPagesLabel.setValue(navigator.getPageAmount());

    }
}
